#include<iostream>
using namespace std;
#include<vector>
#include<queue>

int arrx[4] = { -1,1,0,0 };
int arry[4] = { 0,0,-1,1 };

bool arr[500][500] = { 0 };

int numEnclaves(vector<vector<int>>& grid) {
	int length1 = grid.size(), length2 = grid[0].size();
	
	queue<pair<int,int>>q;
	int number = 0;

	for (int i = 0; i < length1; i++)
	{
		for (int j = 0; j < length2; j++)
		{
			if (i == 0 || j == 0 || i == length1 - 1 || j==length2 - 1)
			{
				if (grid[i][j] == 1)
				{
					arr[i][j] = 1;
					q.push({ i,j });
				}
			}
			else
			{
				if (grid[i][j] == 1)
				{
					number++;
				}
			}
		}
	}

	while (q.size())
	{
		int sz = q.size();
		for (int i = 0; i < sz; i++)
		{
			int x = q.front().first;
			int y = q.front().second;
			q.pop();

			for (int j = 0; j < 4; j++)
			{
				int x1 = x + arrx[j];
				int y1 = y + arry[j];

				if (x1 >= 0 && x1 < length1 && y1 >= 0 && y1 < length2&&!arr[x1][y1])
				{
					if (grid[x1][y1] == 1)
					{
						arr[x1][y1] = 1;
						number--;
						q.push({ x1,y1 });
					}
				}

			}
		}
	}
	return number;
}


int main()
{
	return 0;
}